package com.ruoyi.cloudcourse.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.cloudcourse.domain.CourseSpecial;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.cloudcourse.mapper.FeatherCourseMapper;
import com.ruoyi.cloudcourse.domain.FeatherCourse;
import com.ruoyi.cloudcourse.service.IFeatherCourseService;
import com.ruoyi.common.core.text.Convert;
import org.springframework.transaction.annotation.Transactional;

/**
 * 课程Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-07-13
 */
@Service
public class FeatherCourseServiceImpl implements IFeatherCourseService 
{
    @Autowired
    private FeatherCourseMapper featherCourseMapper;

    /**
     * 查询课程
     * 
     * @param courseId 课程ID
     * @return 课程
     */
    @Override
    public FeatherCourse selectFeatherCourseById(Long courseId)
    {
        return featherCourseMapper.selectFeatherCourseById(courseId);
    }

    /**
     * 查询课程列表
     * 
     * @param featherCourse 课程
     * @return 课程
     */
    @Override
    public List<FeatherCourse> selectFeatherCourseList(FeatherCourse featherCourse)
    {
        return featherCourseMapper.selectFeatherCourseList(featherCourse);
    }

    /**
     * 新增课程
     * 
     * @param featherCourse 课程
     * @return 结果
     */
    @Override
    public int insertFeatherCourse(FeatherCourse featherCourse)
    {
        return featherCourseMapper.insertFeatherCourse(featherCourse);
    }

    /**
     * 修改课程
     * 
     * @param featherCourse 课程
     * @return 结果
     */
    @Override
    public int updateFeatherCourse(FeatherCourse featherCourse)
    {
        return featherCourseMapper.updateFeatherCourse(featherCourse);
    }

    /**
     * 删除课程对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteFeatherCourseByIds(String ids)
    {
        return featherCourseMapper.deleteFeatherCourseByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除课程信息
     * 
     * @param courseId 课程ID
     * @return 结果
     */
    @Override
    public int deleteFeatherCourseById(Long courseId)
    {
        return featherCourseMapper.deleteFeatherCourseById(courseId);
    }

    @Override
    public List<FeatherCourse> selectListChooseBySpecialId(int specialId) {
        return featherCourseMapper.selectListChooseBySpecialId(specialId);
    }

    @Transactional
    @Override
    public int insertTwoTable(FeatherCourse featherCourse, int[] specialIds) {
        int result = insertFeatherCourse(featherCourse);
        int courseId = featherCourseMapper.selectCourseIdByCourseTitle(featherCourse.getCourseTitle());
        List<CourseSpecial> list = new ArrayList<CourseSpecial>();
        for (int i=0;i<specialIds.length;i++){
            CourseSpecial courseSpecial = new CourseSpecial(courseId,specialIds[i]);
            list.add(courseSpecial);
        }
        if (list.isEmpty()){
            return 0;
        }
        featherCourseMapper.insertTwoTable(list);
        return result;
    }

    @Transactional
    @Override
    public int updateTwoTable(FeatherCourse featherCourse, int[] specialIds) {
        int result = updateFeatherCourse(featherCourse);
        Long courseId = featherCourse.getCourseId();
        featherCourseMapper.deleteTwoTableByCourseId(courseId);

        List<CourseSpecial> list = new ArrayList<CourseSpecial>();
        for (int i=0;i<specialIds.length;i++){
            CourseSpecial courseSpecial = new CourseSpecial(courseId.intValue(),specialIds[i]);
            list.add(courseSpecial);
        }
        if (list.isEmpty()){
            return 0;
        }

        featherCourseMapper.insertTwoTable(list);
        return result;
    }

    @Transactional
    @Override
    public int deleteTwoTableByCourseIds(String ids) {
        String[] idsArray = Convert.toStrArray(ids);
        int result = featherCourseMapper.deleteFeatherCourseByIds(idsArray);
        featherCourseMapper.deleteTwoTableByCourseIds(idsArray);
        return result;
    }
}
